import { defineStore } from 'pinia'
import { ref } from 'vue'
import { getBanners } from '@/api/banner'
import type { bannerItem } from '@/types/banner'
export const useBannerStore = defineStore('bannerList', () => {
  const data = ref<bannerItem[]>([])
  function setData(payload: bannerItem[]) {
    data.value = payload
  }
  async function fetchBanner() {
    //优化，不必重新请求借口
    if (data.value.length) {
      return data.value
    }
    const resp = await getBanners()
    console.log(resp, 'banner')
    const activeBannerList = resp.filter((item) => item.active)
    setData(activeBannerList)
    //返回激活的banner
    return activeBannerList
  }

  return {
    fetchBanner
  }
})
